<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 17:14:35 apple $
 页面实现相关函数
*/
namespace core;
defined('BY_JYA') or exit('error');
class template{
	public function get_html($siteid,$type){
	//获取站点内容页模板
		$website=pdo_get('core_mobile_web',array('id'=>$siteid,'uniacid'=>$_SESSION['uniacid']),array('head_html','foot_html','pages_html'));
		$pages_html=json_decode($website['pages_html'],true);
		if($website){
			$return['siteid']=$siteid;
			$return['head_html']=$website['head_html'];
			$return['foot_html']=$website['foot_html'];
			$return['type']=$pages_html[$type];
		}
		return $return;
	
	}
	public function get_customtpl($access,$pagedata=''){
		global $_RQ,$_AR;
		$mainsource='';
		if($access['plugin']!='core'){
			//判断是否来自商城
			if($access['plugin']=='jy_weishop' && $_SESSION['os']=='windows'){
				$pc_set=plugin('jy_weishop')->plugin_setting('pc_set');
				if($pc_set['no_pc']==1){			
					$file = ROOT_D . "/{$_SESSION['this']}/templates/default/mobile/common/main.html";	
				}elseif($pc_set['temp_name']){
					$file=ROOT_PLUGIN.'temp/pc/'.$pc_set['temp_name'].'/custom/pc/common/main.html';
				}else{
					$file=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/'.$_SESSION['os'].'/common/main.html';
				}
			}else{
				$file=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/'.$_SESSION['os'].'/common/main.html';
			}
			if(is_file($file)){
				$mainsource=$file;
			}
			//判断页面html文件是否存在
			$pagehtml=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/'.$_SESSION['os'].'/'.str_replace('.','/',$access['action']).'.html';			
			if(!is_file($pagehtml)){
				$mobilehtml=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/mobile/'.str_replace('.','/',$access['action']).'.html';	
				if(is_file($mobilehtml)){
					$_SESSION['os']='mobile';
					$file=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/mobile/common/main.html';
					if(is_file($file)){
						$mainsource=$file;
					}else{
						$mainsource = ROOT_D . "/{$_SESSION['this']}/templates/default/mobile/common/main.html";			
					}
				}
			}
		}
		if(!$mainsource){
			$mainsource=ROOT_D . "/{$_SESSION['this']}/templates/default/{$_SESSION['os']}/common/main.html";
			//判断是不是DIY页面
			if($_SESSION['os'] =='windows' && $access['plugin']=='core' && $access['action'] == 'welcome'){
				if($_RQ['id']){
					$id=intval($_RQ['id']);
				}else{
					$id=uni_setting('default_mobile_web');
				}
				$pc_html=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('title','html','pc_html','nopc'));		
				if(empty($pc_html['pc_html']) || $pc_html['nopc']){
					$ismobile=true;
				}
			}
			//判断页面html文件是否存在
			if($access['plugin']=='core'){
				$pagehtml=ROOT_D.'/'.$_SESSION['this'].'/templates/default/'.$_SESSION['os'].'/'.str_replace('.','/',$access['action']).'.html';	
			}else{
				if($access['plugin']=='jy_weishop' && $_SESSION['os']=='windows'){
					$pc_set=plugin('jy_weishop')->plugin_setting('pc_set');
					if($pc_set['no_pc']==1){
						$pagehtml=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/mobile/'.str_replace('.','/',$access['action']).'.html';	
					}elseif($pc_set['temp_name'] && $_SESSION['this']=='custom'){
						$pagehtml=ROOT_PLUGIN.'temp/pc/'.$pc_set['temp_name'].'/custom/pc/'.str_replace('.','/',$access['action']).'.html';
					}else{
						$pagehtml=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/'.$_SESSION['os'].'/'.str_replace('.','/',$access['action']).'.html';
					}
				}else{
					$pagehtml=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/'.$_SESSION['os'].'/'.str_replace('.','/',$access['action']).'.html';
				}
			}
			if(!is_file($mainsource) || $ismobile){
				$_SESSION['os']='mobile';
				$mainsource = ROOT_D . "/{$_SESSION['this']}/templates/default/mobile/common/main.html";
			}
		}
		if(!is_file($mainsource)){
			exit('error:template source needed is not exist!');
		}
		$html=file_get_contents($mainsource);
		//获取主题颜色
		if(intval($_RQ['link_webid'])){
			$syscolor=pdo_getcolumn('core_mobile_web',array('id'=>intval($_RQ['link_webid']),'uniacid'=>$_SESSION['uniacid']),'syscolor');
		}elseif($site['syscolor']){
			$syscolor=$site['syscolor'];
		}elseif($_SESSION['account']['syscolor']){
			$syscolor=$_SESSION['account']['syscolor'];
		}else{
			$syscolor='';
		}
		if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core'){
			if(plugin($_SESSION['current_plugin'])->syscolor){
				$syscolor= plugin($_SESSION['current_plugin'])->syscolor;
			}
		}
		$_SESSION['syscolor']=$syscolor;
		$main=array(
			'siteroot'=>SITEROOT,
			'logo'=>$_SESSION['account']['logo'],
			'llogo'=>$_SESSION['account']['llogo'],
			'js_support'=>$_SESSION['account']['js_support']?:'XXX',
			'js_supportimg'=>$_SESSION['account']['js_supportimg'],
			'syscolor'=>$syscolor,
			'hovercolor'=>$_SESSION['account']['hovercolor'],
			'ewm'=>$_SESSION['account']['ewm'],
			'statuscolor'=>$_SESSION['account']['statuscolor'],
			'openbackpage'=>$_SESSION['account']['openbackpage'],
		);
		//读取插件底部菜单
		$main['current_plugin']=$_SESSION['current_plugin'];
		if($_SESSION['current_plugin']){
			if(is_file(ROOT_PLUGIN.$_SESSION['current_plugin'].'/model/custom_mobile_menu.php')){
				$main['menus']=plugin($_SESSION['current_plugin'])->custom_mobile_menu();	
			}else{
				if(is_file(ROOT_PLUGIN.$access['plugin'].'/model/custom_mobile_menu.php')){
					$main['menus']=plugin($access['plugin'])->custom_mobile_menu();	
				}
			}
		}
		//如果菜单为空就获取当前插件的
		if(empty($main['menus']) && $access['plugin']!='core'){
			if(is_file(ROOT_PLUGIN.$access['plugin'].'/model/custom_mobile_menu.php')){
				$main['menus']=plugin($access['plugin'])->custom_mobile_menu();
			}
		}
		if($_SESSION['os']=='windows' && $access['plugin']!='core' && $_SESSION['uid'] && is_file(ROOT_PLUGIN.$access['plugin'].'/model/custom_menu.php')){
			$main['framess']=cfc($access['plugin'])->custom_menu();
		}
		if($access['plugin']=='jy_weishop' && $_SESSION['this']=='custom'){
			$pc_set=plugin('jy_weishop')->plugin_setting('pc_set');
			if($pc_set['no_pc']==1){
				$main['no_pc']=true;
			}
			if($pc_set['temp_name']){
				$main['pc_temp_name']=$pc_set['temp_name'];
			}
		}
		$main['show_lang']=$_SESSION['show_lang']?:'ch';
		$main['pagedata']=$pagedata;
		$search=html_config($access,$main);
		$search['icon']=$_AR['page']['icon']?tomedia($_AR['page']['icon']):'../core/resource/favicon.ico';
		$page_title=$_SESSION['page_title']?:$_SESSION['account']['title'];
		$page_title=$pagedata?$page_title:'';
		$search['page']=array(
			'title'=>$page_title,
			'icon'=>$search['icon'],
			'keywords'=>'',
			'description'=>'',
		);
		$search['share']=$_AR['share']?:'{}';
		$search['jssdk']=$_AR['jssdk']?:'{}';
		$search['plugin_head']='';
		if($access['plugin']!='core'){
			$file=ROOT_PLUGIN.$access['plugin'].'/'.$_SESSION['this'].'/templates/'.$_SESSION['os'].'/common/head.html';
			if(is_file($file)){
				$search['plugin_head']=file_get_contents($file);
				$search['plugin_head']=str_replace('{plugin_resource}',SITEROOT.'addons/'.$access['plugin'].'/resource/',$search['plugin_head']);
			}
		}
		$res=array('search'=>$search,'html'=>$html);
		return $res;
	}
	public function get_modulemenus($plugin){
		$module=pdo_get('core_plugins',array('name'=>$plugin));
		$covers=$module['covers']?json_decode($module['covers'],true):array();
		if($covers){
			foreach($covers as &$cover){
				if($module['type']==4){
					$cover['name']=$cover['name']?:$cover['do'];
				}	
				$cover['url']=array('plugin'=>$module['name'],'action'=>'basic.module_home','op'=>'cover','query'=>'ac='.$cover['name'].'&type='.$module['type']);
				unset($cover['name']);
			}
		}
		if($module['settings']){
			$covers[]=array('title'=>'参数设置','url'=>array('action'=>'setting','plugin'=>$module['name']));
		}
		$menus=json_decode($module['menus'],true);
		if($menus){
				foreach($menus as &$menu){
					if($module['type']==4){
						$menu['name']=$menu['name']?:$menu['do'];
					}
					$menu['url']='./index.php?action=module&do='.$menu['name'].'&m='.$module['name'];
					unset($menu['name']);
				}
		}
		$menu_list=array();
		if($covers){
			$menu_list[]=array('title'=>'基础功能设置','child'=>$covers);
		}
		if($menus){
			$menu_list[]=array('title'=>'插件业务菜单','child'=>$menus);
		}
		return $menu_list;
	}
	public function check_ssrhtml($actionhtml,$os='',$ssr=false){
		$os=$os?:$_SESSION['os'];
		$os=$os?:'windows';
		$ssr=$ssr?"/ssr/":"/";
		if(ACCESS_PLUGIN=='core'){
					$file=ROOT_D.'/'.$_SESSION['this'].'/templates/default/'.$os.$ssr.$actionhtml.'.html';
					if(!is_file($file)){
						$os=$os=='mobile'?'windows':'mobile';
						$file=ROOT_D.'/'.$_SESSION['this'].'/templates/default/'.$os.$ssr.$actionhtml.'.html';
					}
		}else{
					$file=ROOT_D.'/addons/'.ACCESS_PLUGIN.'/'.$_SESSION['this'].'/templates/'.$os.$ssr.$actionhtml.'.html';
					if(!is_file($file)){
						$os=$os=='mobile'?'windows':'mobile';
						$file=ROOT_D.'/addons/'.ACCESS_PLUGIN.'/'.$_SESSION['this'].'/templates/'.$os.$ssr.$actionhtml.'.html';
					}
		}
		return $file;
	}
	public function template($html,$os='',$ssr=false){
		$os=$os?:$_SESSION['os'];
		$os=$os?:'windows';
		$file=cfc('template')->check_ssrhtml($html,$os,$ssr);
		if(ACCESS_PLUGIN=='core'){
			$temp=ROOT_AT.'templates/core/'.$_SESSION['this'].'/'.$os.'/'.$html.'.html';
		}else{
			$temp=ROOT_AT.'templates/addons/'.ACCESS_PLUGIN.'/'.$_SESSION['this'].'/'.$os.'/'.$html.'.html';
		}
		if(!is_file($file)){
			exit('找不到页面模版文件！'.$file);
		}
		if(is_file($temp) && filemtime($temp)>filemtime($file)){
			return $temp;
		}
		$str=file_get_contents($file);
		$str = preg_replace('/<!--{(.+?)}-->/s', '{$1}', $str);
		$str = preg_replace('/{template\s+(.+?)}/', '<?php include cfc(\'template\')->template($1)?>', $str);
		$str = preg_replace('/{php\s+(.+?)}/', '<?php $1?>', $str);
		$str = preg_replace('/{echo\s+(.+?)}/', '<?php echo $1;?>', $str);
		$str = preg_replace('/{if\s+(.+?)}/', '<?php if($1) { ?>', $str);
		$str = preg_replace('/{else}/', '<?php } else { ?>', $str);
		$str = preg_replace('/{else ?if\s+(.+?)}/', '<?php } else if($1) { ?>', $str);
		$str = preg_replace('/{\/if}/', '<?php } ?>', $str);
		$str = preg_replace('/{loop\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2) { ?>', $str);
		$str = preg_replace('/{loop\s+(\S+)\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2 => $3) { ?>', $str);
		$str = preg_replace('/{\/loop}/', '<?php } } ?>', $str);
		$str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}/', '<?php echo $1;?>', $str);
		$str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\[\]\'\"\$]*)}/', '<?php echo $1;?>', $str);
		$str = preg_replace('/{access_json\s+(\S+)}/', '<?php echo access_json($1);?>', $str);
		$str = preg_replace('/{manage_url\s+(\S+)}/', '<?php echo manage_url($1);?>', $str);
		$str = preg_replace('/{custom_url\s+(\S+)}/', '<?php echo custom_url($1);?>', $str);
		$str = preg_replace('/{custom_url\s+(\S+)\s+(array\(.+?\))}/', '<?php echo custom_url($1, $2);?>', $str);
		$str = preg_replace('/{tomedia\s+(\S+)}/', '<?php echo tomedia($1);?>', $str);
		$str = preg_replace('/{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)}/s', '<?php echo $1;?>', $str);
		$str = str_replace('{##', '{', $str);
		$str = str_replace('##}', '}', $str);
		$str = "<?php defined('BY_JYA') or exit('error');?>" . $str;
		cfc('files')->mkdirs(dirname($temp));
		file_put_contents($temp,$str);
		return $temp;
	}
}